﻿using Microsoft.Phone.Shell;
using Microsoft.Xna.Framework;
using MS.TravelJourney.Final.ViewModels;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace MS.TravelJourney.Final.Core
{
    public class TileCore
    {
        public static bool TileExists(string NavSource)
        {
            ShellTile tile = ShellTile.ActiveTiles.FirstOrDefault(o => o.NavigationUri.ToString().Contains(NavSource));
            return tile == null ? false : true;
        }

        public static void DeleteTile(string NavSource)
        {
            ShellTile tile = ShellTile.ActiveTiles.FirstOrDefault(o => o.NavigationUri.ToString().Contains(NavSource));
            if (tile == null) return;

            tile.Delete();
        }

        public static void SetTile(ItemViewModel item, string NavSource)
        {
            FlipTileData tileData = new FlipTileData()
            {
                //Front square data
                Title = item.Name,
                BackgroundImage = new Uri(item.Photo, UriKind.Relative),
                SmallBackgroundImage = new Uri(item.Photo, UriKind.Relative),

                //Back square data
                BackTitle = item.Name,
                BackContent = item.Desc,
                BackBackgroundImage = new Uri(item.Photo, UriKind.Relative),

                //Wide tile data
                WideBackgroundImage = new Uri(item.Photo, UriKind.Relative),
                WideBackBackgroundImage = new Uri(item.Photo, UriKind.Relative),
                WideBackContent = item.Desc
            };

            IconicTileData iconData = new IconicTileData 
            {
                BackgroundColor = System.Windows.Media.Color.FromArgb(255, 195, 61, 39),
                Count = 3,
                IconImage = new Uri(item.Photo,UriKind.RelativeOrAbsolute),
                Title = item.Name,
                WideContent1 = item.Desc.Substring(0,10),
                WideContent2 = item.Desc.Substring(10,15),
                WideContent3 = item.Desc.Substring(15,30)
            };

            CycleTileData cycle = new CycleTileData
            {
                Count = 9,
                CycleImages = new List<Uri> { 
                    new Uri("Assets/About.png",UriKind.RelativeOrAbsolute),
                    new Uri("Assets/Aerial.png",UriKind.RelativeOrAbsolute),
                    new Uri("Assets/Aeroplane.png",UriKind.RelativeOrAbsolute),
                    new Uri("Assets/Attachment.png",UriKind.RelativeOrAbsolute)
                },
                SmallBackgroundImage = new Uri("Road.png",UriKind.RelativeOrAbsolute),
                Title = item.Name
            };

            ShellTile.Create(new Uri(NavSource, UriKind.Relative), cycle, true);
        }

        private static string makeString(ObservableCollection<string> Ingredients)
        {
            string res = "";

            foreach (var ingredient in Ingredients)
            {
                res += ingredient + "\n";
            }

            return res;
        }


        public static void UpdateMainTile(ItemViewModel group)
        {
            //Get application's main tile
            var mainTile = ShellTile.ActiveTiles.Last();

            if (null != mainTile)
            {
                IconicTileData tileData = new IconicTileData()
                {
                    Count = 8,
                    BackgroundColor = System.Windows.Media.Color.FromArgb(255, 195, 61, 39),
                    Title = "丽江",
                    IconImage = new Uri("/Assets/MediumLogo.png", UriKind.RelativeOrAbsolute),
                    SmallIconImage = new Uri("/Assets/SmallLogo.png", UriKind.RelativeOrAbsolute),
                    WideContent1 = "Recent activity:",
                    WideContent2 = "Browsed " + group.Name + " group",
                    WideContent3 = "with total of " + 10 + " places"
                };

                mainTile.Update(tileData);
            }
        }
    }

}
